//  1.构造一副扑克牌
//  2.揭牌

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Card {
    public String suit;//花色
    public int rank;//牌面

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }
    public Card() {
    }

    @Override
    public String toString() {
        return "[" + suit + ":" + rank + "]";
//        return String.format("[%s %d]", suit, rank);
    }
}
public class PlayCard {
    public static final String[] suits = {"♥", "♠", "♦", "♣"};
//    拿到牌
    private static List<Card> buyCart() {
        List<Card> deck = new ArrayList<>(52);
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j < 14; j++) {
//                1.
//                String suit = suits[i];
//                int rank = j;
//                Cart cart = new Cart();
//                cart.suit = suit;
//                cart.rank = rank;
//                deck.add(cart);
//                2.
                deck.add(new Card(suits[i],j));
            }
        }
        return deck;
    }
//    洗牌
    public static void shuffle(List<Card> deck) {
        int size = deck.size();
        for (int i = size-1; i > 0 ; i--) {
            Random random = new Random();
            int j = random.nextInt(i);
            swap(deck,i,j);
        }
    }
//    洗牌
    public static void swap(List<Card> deck, int i, int j) {
//        1.error
//        Cart tmp = deck[i];
//        deck[i] = deck[j];
//        deck[j] = tmp;
//        error   不是数组  现在是在面向对象
//        2.
        Card tmp = deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,tmp);
    }

    public static void main(String[] args) {
//        买牌
        System.out.print("买牌：");
        List<Card> cards = buyCart();
        System.out.println(cards);
//        洗牌
        System.out.print("洗牌：");
        shuffle(cards);
        System.out.println(cards);
//        揭牌  3个人每个人轮流揭5张牌
        System.out.println("揭牌:3个人每个人轮流揭5张牌");
        List<List<Card>> hand = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
//        每个人轮流揭牌
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);
                hand.get(j).add(card);
            }
        }
        System.out.println("cat："+hand1);
        System.out.println("dog："+hand2);
        System.out.println("pig："+hand3);
        System.out.println("leftover："+cards);
    }

}
